﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace Hosting.Class
{
    public class CategoryTitle
    {

        public CategoryTitle() { }

        #region [Select all category Title]
        /// <summary>
        /// Select all category Title
        /// </summary>
        /// <param name="getActive">
        /// <para>0     : false</para>
        /// <para>1     : true</para>
        /// <para>-1    : get all</para>
        /// </param>
        /// <returns>
        /// <para>Return all category Title data</para>
        /// <para>null  : error</para>
        /// </returns>
        public List<PC_CategoryTitle_getAllResult> getAll(string getActvie)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var list = db.PC_CategoryTitle_getAll(Int32.Parse(getActvie)).ToList();
                return list;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region [Insert Category Title]
        /// <summary>
        /// Insert a new Category Title
        /// </summary>
        /// <param name="name">Name of category title</param>
        /// <param name="description">Description of category title</param>
        /// <param name="active">Visible/Hidden category title on interface</param>
        /// <returns>0: successfully</returns>
        /// <returns>-99: errror</returns>
        public int add(string name, string description, bool active)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var isInsert = db.PC_CategoryTitle_Ins(name, description, active);
                return isInsert;
            }
            catch
            {
                return -99;
            }
        }
        #endregion

        #region [Insert is valid or not]
        /// <summary>
        /// Check insert is valid or not
        /// </summary>
        /// <param name="name">name of category title</param>
        /// <param name="description">A short description about category title</param>
        /// <param name="active">Visible/hidden category title and category detail on interface</param>
        /// <returns>1: successfully</returns>
        /// <returns>10: Name is null or empty</returns>
        /// <returns>11: Name is greater than 2000 characters</returns>
        /// <returns>20: Description is greater than 2000 chareacters</returns>
        public int addValid(string name, string description)
        {
            if(String.IsNullOrEmpty(name))
            {
                return 10;
            }
            else if(name.Length > 2000)
            {
                return 11;
            }
            else if(description.Length > 2000)
            {
                return 20;
            }

            return 1;
        }
        #endregion

        #region [Update Category Title]
        /// <summary>
        /// Update Category Title
        /// </summary>
        /// <param name="id">ID of Category Title</param>
        /// <param name="name">Name of Category Title</param>
        /// <param name="description">A short Description about Category Title</param>
        /// <param name="active">Visible/Hidden Category Title on Interface</param>
        /// <returns>0: successfully</returns>
        /// <returns>-99: errror</returns>
        public int edit(string id, string name, string description, bool active)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var isUpdate = db.PC_CategoryTitle_Upd(Int32.Parse(id), name, description, active);
                return isUpdate;
            }
            catch
            {
                return -99;
            }
        }
        #endregion

        #region [Update is valid or not]
        /// <summary>
        /// Update is valid or not
        /// </summary>
        /// <param name="id">ID of Category Title</param>
        /// <param name="name">Name of Category Title</param>
        /// <param name="description">A short Description about Category Title</param>
        /// <returns>1: successfully</returns>
        /// <returns>2: Id is null or empty</returns>
        /// <returns>3: Id is not valid</returns>
        /// <returns>10: Name is null or empty</returns>
        /// <returns>11: Name is greater than 2000 characters</returns>
        /// <returns>20: Description is greater than 2000 chareacters</returns>
        public int editValid(string id, string name, string description)
        {
            if (String.IsNullOrEmpty(id))
            {
                return 2;
            }

            Regex rId = new Regex("^[0-9]{1,}$");
            Match mId = rId.Match(id);
            if (!mId.Success)
            {
                return 3;
            }
            if (String.IsNullOrEmpty(name))
            {
                return 10;
            }
            else if (name.Length > 2000)
            {
                return 11;
            }
            else if (description.Length > 2000)
            {
                return 20;
            }

            return 1;
        }
        #endregion

        #region [Delete an existed Category Title]
        /// <summary>
        /// Delete an existed Category Title
        /// </summary>
        /// <param name="id">ID of Category Title</param>
        /// <returns></returns>
        public int delete(string id)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var isDelete = db.PC_CategoryTitle_Del(Int32.Parse(id));
                return isDelete;
            }
            catch (Exception ex)
            {
                return -99;
            }
        }
        #endregion

    }
}